package com.zhdl.hardware.motor.leisai.service.impl;

import com.zhdl.hardware.motor.leisai.repository.LeisaiPortCmdHandleDao;
import com.zhdl.hardware.motor.leisai.service.LSCmdSendService;
import com.zhdl.modules.tools.CommonCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;

@Slf4j
@Service
public class LSCmdSendServiceImpl implements LSCmdSendService {

    @Resource
    public ThreadPoolTaskScheduler taskScheduler1;
    @Resource
    public LeisaiPortCmdHandleDao handleDao;
    Map<String, ScheduledFuture<?>> mapSF = new HashMap<>();

    /**
     * 读取电机当前位置
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiReadCurrentLocation(String port, String slaveStationId, CommonCallback<String> callback) {
        ScheduledFuture<?> scheduledFuture = taskScheduler1.scheduleWithFixedDelay(() -> {
            handleDao.leisaiReadCurrentLocation(port, slaveStationId, s -> {
                callback.statusCallback(s);
                log.info("");
            });
        }, 500);
        mapSF.put("no1", scheduledFuture);
    }

    /**
     * 读取电机报警信息
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiReadAlarmMessage(String port, String slaveStationId, CommonCallback<String> callback){
        ScheduledFuture<?> scheduledFuture = taskScheduler1.scheduleWithFixedDelay(() -> {
            handleDao.leisaiReadAlarmMessage(port, slaveStationId, s -> {
                callback.statusCallback(s);
                log.info("");
            });
        }, 2000000);
        mapSF.put("no2", scheduledFuture);
    }

    @Override
    public void cancelScheduledFuture() {
        ScheduledFuture<?> scheduledFuture1 = mapSF.get("no1");
        scheduledFuture1.cancel(true);
        mapSF.remove("no1");
    }

    /**
     * 设置电机移动距离
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     * @param distance       距离
     */
    @Override
    public void leisaiSetMoveDist(String port, String slaveStationId, int distance) {
        handleDao.leisaiSetMoveDist(port, slaveStationId, distance);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + slaveStationId + ",距离为_" + distance);
    }

    @Override
    public void leisaiSetMotorModel(String port,String salveId,int model){
        handleDao.leisaiSetMotorModel(port, salveId, model);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + salveId + ",模式为" + (model == 1?"绝对模式":"速度模式"));
    }

    /**
     * 雷赛电机-电机移动速度
     *
     * @param slaveStationId 从站Id
     * @param speed          写入值
     */
    @Override
    public void leisaiSetMoveSpeed(String port, String slaveStationId, int speed) {
        handleDao.leisaiSetMoveSpeed(port, slaveStationId, speed);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + slaveStationId + ",速度为_" + speed);
    }

    /**
     * 雷赛电机-电机急停
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiEmergencyStop(String port, String slaveStationId) {
        handleDao.leisaiEmergencyStop(port, slaveStationId);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + "01");
    }
    @Override
    public void leisaiReady(String port, String slaveStationId) {
        handleDao.leisaiReady(port, slaveStationId);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + "01");
    }

    /**
     * 雷赛电机-当前报警复位
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiNowWarnReset(String port, String slaveStationId) {
        handleDao.leisaiNowWarnReset(port, slaveStationId);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + "01");
    }

    /**
     * 雷赛电机-历史报警复位
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiLastWarnReset(String port, String slaveStationId) {
        handleDao.leisaiLastWarnReset(port, slaveStationId);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + "01");
    }
}
